import 'package:gold/base/view_model.dart';
import 'package:gold/http/api_rank.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/pool_list_model.dart';
import 'package:gold/model/pool_model.dart';
import 'package:gold/ui/home/rank/rank_content_viewmodel.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

/// @description:
/// @author tangzhentao
/// @created at 11:35 2024/12/29

class RankHistoryViewmodel extends Viewmodel {
  /// 排行榜类型
  final PoolType _poolType;
  /// 刷新控制器
  final RefreshController controller = RefreshController();
  /// 数据集合
  final List<PoolRankModel> list = [];
  /// 页码
  int _page = 1;
  /// 奖池id
  int? _id;

  RankHistoryViewmodel(this._poolType);

  /// 加载数据
  Future loadFirst() async {
    setLoading();
    _page = 1;
    if (_id != null) {
      await _loadList();
    } else {
      final poolResult = await Http().request<PoolModel>(ApiRank.poolInfo,
          data: {'type': _poolType.type}, showErrorMsg: false);
      if (poolResult.success) {
        final poolId = poolResult.data?.id;
        if (poolId != null) {
          _id = poolId;
          await _loadList();
        } else {
          setEmpty(msg: '奖池信息异常');
        }
      } else {
        setFailed(msg: poolResult.msg);
      }
    }
  }

  /// 加载列表数据
  Future _loadList() async {
    final result = await Http().request<PoolListModel>(ApiRank.poolRank,
        data: {'page': _page, 'id': _id});
    if (result.success) {
      list.clear();
      list.addAll(result.data?.data?? []);
      if (list.isNotEmpty) {
        setSuccess();
        controller.headerMode?.value = RefreshStatus.canRefresh;
        controller.footerMode?.value = _page < (result.data?.totalPages?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      } else {
        setEmpty();
      }
    } else {
      setFailed(msg: result.msg);
    }
  }

  /// 刷新数据
  Future refresh() async {
    _page = 1;
    final result = await Http().request<PoolListModel>(ApiRank.poolRank,
        data: {'page': _page, 'id': _id});
    if (result.success) {
      controller.refreshCompleted();
      controller.footerMode?.value = _page < (result.data?.totalPages?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.data?? [];
      if (data.isNotEmpty) {
        list.clear();
        list.addAll(data);
        setSuccessChange();
      }
    } else {
      controller.refreshFailed();
    }
  }

  /// 加载更多数据
  Future loadMore() async {
    _page++;
    final result = await Http().request<PoolListModel>(ApiRank.poolRank,
        data: {'page': _page, 'id': _id});
    if (result.success) {
      controller.loadComplete();
      controller.footerMode?.value = _page < (result.data?.totalPages?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.data?? [];
      if (data.isNotEmpty) {
        list.addAll(data);
        setSuccessChange();
      } else {
        controller.loadNoData();
      }
    } else {
      controller.loadFailed();
    }
  }

  @override
  onReload() => loadFirst();

  @override
  onRetry() => loadFirst();

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}
